package ru.yermak.bookkeeping.currency;

import ru.yermak.bookkeeping.model.Currency;
import ru.yermak.bookkeeping.common.BookkeepingAction;
import ru.yermak.bookkeeping.common.Command;
import ru.yermak.bookkeeping.ui.Selector;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

/**
 * User: harrier
 * Date: Aug 13, 2008
 */
@Scope("action")
@org.springframework.stereotype.Component
public class CurrencyRemoveCommand implements Command {

    @Autowired
    private CurrencyDao currencyDao;
    @Autowired
    @CurrencyQulifier
    private Selector selector;


    @Transactional(propagation = Propagation.REQUIRED)
    public void execute(ActionEvent e) {
        Integer currencyId = selector.getSelectedId();
        if (currencyId == null) {
            return;
        }

        Currency currency = currencyDao.getById(currencyId);
        int result = JOptionPane.showConfirmDialog(((Component) e.getSource()), "Are you sure?", "Remove currency", JOptionPane.OK_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.OK_OPTION) {
            currencyDao.removeCurrency(currency);
            selector.selectRow(null);
        }
    }
}
